package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.dtos.ResponseResult;
import com.heima.model.enums.AppHttpCodeEnum;
import com.heima.model.userDao.dto.LoginDto;
import com.heima.model.userDao.pojo.ApUser;
import com.heima.model.userDao.vo.LoginVo;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.ApUserService;
import com.heima.utils.common.AppJwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;


/**
 * APP用户信息表(ApUser)表服务实现类
 *
 * @author makejava
 * @since 2023-01-08 13:32:16
 */
@Service
@Slf4j
public class ApUserServiceImpl extends ServiceImpl<ApUserMapper, ApUser> implements ApUserService {


    /**
     * @param dto
     * @return com.heima.model.dtos.ResponseResult
     * @description 用户登录
     * @author 31115
     * @date 2023/1/8 13:41
     */
    @Override
    public ResponseResult login(LoginDto dto) {

        //判断是否为游客
        if (StringUtils.isEmpty(dto.getPhone())&&StringUtils.isEmpty(dto.getPassword())){
            //游客  同样返回token  id = 0
            Map<String, Object> map = new HashMap<>();
            map.put("token", AppJwtUtil.getToken(0L));
            return ResponseResult.okResult(map);
        }

        //校验用户名密码
        if (StringUtils.isEmpty(dto.getPhone())) {
            log.debug("用户未输入手机号:{}", dto.getPhone());
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PHONE_ERROR, "请输入手机号");
        }
        if (StringUtils.isEmpty(dto.getPassword())) {
            log.debug("用户未输入密码:{}", dto.getPassword());
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR, "请输入密码");
        }

        //判断手机号密码是否正确
        if (dto.getPhone().length() != 11) {
            log.debug("用户手机号输入错误:{}", dto.getPassword());
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PHONE_ERROR, "手机号输入错误");
        }

        LambdaQueryWrapper<ApUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ApUser::getPhone,dto.getPhone());
        ApUser user = this.getOne(wrapper);
        if (user!=null){
            //有用户信息 校验密码
            //密码加密
            String salt = user.getSalt(); //盐
            String md5From = DigestUtils.md5DigestAsHex(( dto.getPassword()+salt).getBytes());

            if (user.getPassword().equals(md5From)){
                //登录成功
                LoginVo vo = new LoginVo();
                user.setPassword(null);
                user.setSalt(null);
                vo.setUser(user);
                vo.setToken(AppJwtUtil.getToken(user.getId()));
                log.debug("用户登录成功:{}", user);
                return ResponseResult.okResult(vo);
            }else {
                log.debug("密码不正确:{}", user);
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }
        }else {
            log.debug("未注册:{}", dto.getPassword());
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "未注册");
        }
    }
}

